package edu.arizona.sista.learn.activity.model;

import java.util.Comparator;

import edu.arizona.cs.learn.timeseries.model.Interval;

public class PredicateInterval extends Interval{
	
	private Predicate predicate;
	
	public PredicateInterval(Predicate predicate, int start, int end){
		this.predicate = predicate;
		this.start = start;
		this.end = end;
	}
	
	public PredicateInterval(PredicateInterval interval){
		file = interval.file;
		start = interval.start;
		end = interval.end;
		episode = interval.episode;
		
		predicate = interval.getPredicate().copy();
	}
	
	public PredicateInterval(Interval interval){
		file = interval.file;
		start = interval.start;
		end = interval.end;
		episode = interval.episode;
		
		predicate = Predicate.fromString(interval.name);
	}
	
	public static PredicateInterval make(String name, int s, int e) {
		return new PredicateInterval( Interval.make(name,s,e) );
	}

	public PredicateInterval copy(){
		return new PredicateInterval(this);
	}
	
	public void setPredicateName(String predicateName){
		predicate.setName(predicateName);
	}
	
	public void setPredicate(Predicate pred) {
		predicate = pred;
	}
	
	public Predicate getPredicate() {
		return predicate;
	}
	
	public String getName() {
		return predicate.toString();
	}
	
	public String toString() {
		return "(\"" + getName() + "\" " + start + " " + end + ")";
	}
	
	/**
	 * A generic comparator that will compare two temporal objects in a sortable
	 * fashion. esf --> earliest starting first.
	 */
	public static Comparator<PredicateInterval> esf = new Comparator<PredicateInterval>() {
		public int compare(PredicateInterval o1, PredicateInterval o2) {
			if (o1.start > o2.start)
				return 1;
			if (o1.start < o2.start)
				return -1;

			if (o1.end > o2.end)
				return 1;
			if (o1.end < o2.end)
				return -1;

			String name1 = o1.toString();
			String name2 = o2.toString();

			int compValue = name1.compareTo(name2);
			if (compValue != 0)
				return compValue;

			throw new RuntimeException("Error equals names " + name1 + " "
					+ name2);
		}

	};
}
